package com.funo.business.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.funo.business.dao.TemplateDao;
import com.funo.business.entities.TemplateItem;
import com.funo.common.pojo.QueryCondition;
import com.funo.core.base.BaseDao;

@Repository("templateDao")
public class TemplateDaoImpl extends BaseDao<TemplateItem, String> implements TemplateDao {

	private Criteria getCriteria(TemplateItem item){
		Criteria criteria = super.getCriteria();
		if (item!=null) {
			criteria.add(Restrictions.eq("isValid", new Integer(1)));
			criteria.addOrder(Order.desc("insertDt"));
		}
		return criteria;
	}
	
	@Override
	public Integer count(QueryCondition condition,TemplateItem item) {
		Criteria criteria = getCriteria(item);
		return super.countByCriteria(criteria);
	}

	@Override
	public List<TemplateItem> pageList(QueryCondition condition,TemplateItem item) {
		// TODO Auto-generated method stub
		Criteria criteria = getCriteria(item);
		criteria.setFirstResult(condition.getStart());
		criteria.setMaxResults(condition.getLimit());
		return super.findByCriteria(criteria);
	}


}
